package com.songtech.server.Calculation;

import com.songtech.server.Point.PosPoint;
import com.songtech.server.utils.CalculationUtils;

/**
 * Create By YINN on 2018/4/28 18:14
 * Description :
 */
public class TrailPointGrepCalculation {

    private static final int timeDiff = 10;

    //距离变动范围(米)
    private static final double MIN_CHANGE_DISTANCE = 50;

    //常人奔跑速度极限 20 - 30 km/h,此处取30km/h = 8.333m/s
    private static final double PERSON_MAX_SPEED = 8.333;

    //450km/h 10S民航飞机极限位移m
    private static final double AIR_MAX_DISTANCE_10S = 1250;



    protected PosPoint ori;

    protected PosPoint present;

    protected Double distance;

    public TrailPointGrepCalculation(PosPoint ori, PosPoint present) {
        this.ori = ori;
        this.present = present;
    }

    public TrailPointGrepCalculation(PosPoint ori, PosPoint present, double distance) {
        this.ori = ori;
        this.present = present;
        this.distance = distance;
    }


    /**
     * @return true  需要过滤(不入库)
     * false 入库
     */
    public boolean isGrepPresentPoint() {

        //lbs 不过滤
        if (ori.getSpeed() == null || present.getSpeed() == null){
            return false;
        }

        //两点其中有一个速度大于零，进入轨迹过滤算法
        if (ori.getSpeed() > 0 || present.getSpeed() > 0) {

            //判断是否为行车状态
            if (ori.getSpeed() > PERSON_MAX_SPEED || present.getSpeed() > PERSON_MAX_SPEED){
                return false;
            }

            if (distance == null){
                this.distance = CalculationUtils.getDistance(ori.getLat(), ori.getLon(),
                        present.getLat(), present.getLon());
            }

            //判断是否10s内点位飘逸超过正常情况下最大可移动距离(是否出现伪登陆情况)
            if (distance > AIR_MAX_DISTANCE_10S){

                return false;

            }

            if (distance > 0) {

                //获取偏移量 : 匀加速运动下位移 减 经纬度之间的距离 的绝对值
                double deviation = Math.abs(calDistanceUniformlyAcceleratedMotion(ori, present) - distance);

                if (deviation > 0.5 * distance){

                    return true;

                }

                return false;
            }

        }

        return true;
    }


    //人平均每分钟 步行80左右
    //默认为匀加速直线运动
    //s=V0t+1/2at^2 = v0t + 0.5 * t^2 (v1 - v0)/t = 0.5 * v0t +  0.5 * v1t
    private double calDistanceUniformlyAcceleratedMotion(PosPoint ori, PosPoint present) {

        return 0.5 * (ori.getSpeed() + present.getSpeed()) * timeDiff;

    }

}
